package cn.wolfcode.controller;

import cn.wolfcode.annotation.RequiredPermission;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.result.JsonResult;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private ApplicationContext ctx;

    //@RequiresPermissions(value={"permission:list","权限页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo")QueryObject qo, Model model){

        PageInfo<Permission> pageInfo = permissionService.list(qo);
        model.addAttribute("result",pageInfo);
        return "permission/list";
    }

    //对权限的重新加载
    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){

        //获取数据库中的所有权限
        List<Permission> permissions = permissionService.selectAll();
        Map<String,String> permap = new HashMap<>();
        for (Permission permission:permissions){
            permap.put(permission.getName(),permission.getExpression());
        }

        Map<String,Object> map = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> controllers = map.values();
        for (Object controller : controllers){
            Class<?> clzz = controller.getClass();
            Method[] methods = clzz.getDeclaredMethods();
            for (Method method:methods){
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                if(annotation!=null){
                    String name = annotation.name();
                    String expression = annotation.experssion();
                    if (permap.get(name)==null){
                        permissionService.insert(new Permission(null,name,expression));
                    }
                }
            }
        }
        return JsonResult.success(null);
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission){
        if (permission.getId()==null){
            permissionService.insert(permission);
        }else {
            permissionService.updateByPrimaryKey(permission);
        }
        return "redirect:/permission/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        permissionService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }
}
